#!/bin/bash

#echo "[TASK 1] Create /mnt/fast-disks Directory For Local Volume Provisioning"
#mkdir /mnt/fast-disks
#chmod 777 -R /mnt/fast-disks
#for i in `seq 1 20`;
#	do
#		DIRNAME="vol$i"
#		mkdir -p /mnt/fast-disks/$DIRNAME 
#		chcon -Rt svirt_sandbox_file_t /mnt/fast-disks/$DIRNAME
#		chmod 777 /mnt/fast-disks/$DIRNAME
#       mount -t tmpfs -o size=10G $DIRNAME /mnt/fast-disks/$DIRNAME
#		echo "created directory $DIRNAME"
#done


#for i in `seq 21 40`;
#	do
#		DIRNAME="vol$i"
#		mkdir -p /mnt/fast-disks/$DIRNAME 
#		chcon -Rt svirt_sandbox_file_t /mnt/fast-disks/$DIRNAME
#		chmod 777 /mnt/fast-disks/$DIRNAME
 #       mount -t tmpfs -o size=5G $DIRNAME /mnt/fast-disks/$DIRNAME
#		echo "created directory $DIRNAME"
#done


# Join Worker Nodes To The Kubernetes Cluster
echo "Join Node To Kubernetes Cluster"
sudo yum install -y http://mirror.centos.org/centos/8-stream/AppStream/x86_64/os/Packages/sshpass-1.09-4.el8.x86_64.rpm
sudo sshpass -p "$1" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $2:/join_worker_node.sh /join_worker_node.sh
bash /join_worker_node.sh


echo "Copy Kube Config To Vagrant User .kube Directory"
sudo sshpass -p "$1" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $2:/etc/kubernetes/admin.conf /etc/kubernetes/admin.conf
mkdir /home/vagrant/.kube
cp /etc/kubernetes/admin.conf /home/vagrant/.kube/config
chown -R vagrant:vagrant /home/vagrant/.kube


echo "Adjust Kubelet IP"
echo KUBELET_EXTRA_ARGS=\"--node-ip=`ip addr show eth1 | grep inet | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}/" | tr -d '/'`\" > /etc/sysconfig/kubelet
systemctl restart kubelet